/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Swing;

import enums.CustomerEnum;
import common.CustomerUtils;
import entities.Customer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author A
 */
public class CustomerScreen {
    Object[][] rows;
    private final int customerColumn = 3;
    private final int customerRows = 10;
    private final String customerTabName = "Nowy Klient";
    private final String customerTabNameEdition = "Edycja klienta";
    private final String customerMainTab = "Klient";
    private JTextField txtFirstName, txtLastName, txtStreet, txtZipCode, txtCity, txtPhone, txtEmail, txtNIP;
    private JLabel lblWarnFirstName, lblWarnLastName, lblWarnStreet, lblWarnZipCode, lblWarnCity, lblWarnPhone, lblWarnEmail, lblWarnNIP;
    private CustomerUtils utils = new CustomerUtils();
    
 public void populateKlientTable(JTable KlientTable, JPanel KlientPanel, 
         final JTextField CustOptTxtField){
    String[] columns = { "ID", "Imię", "Nazwisko", "Ulica", "Kod pocztowy",
    "Miejscowość", "Telefon", "Email", "NIP", "Data utworzenia", "Data modyfikacji"};
    ArrayList<Customer> customerData = new CustomerUtils().getCustomerData();
    rows = new Object[customerData.size()][columns.length];
    for(int i = 0 ; i < customerData.size() ; i++){
            rows[i][0] = customerData.get(i).getCustomerId() ;
            rows[i][1] = customerData.get(i).getFirstName();
            rows[i][2] = customerData.get(i).getLastName();
            rows[i][3] = customerData.get(i).getStreet();
            rows[i][4] = customerData.get(i).getPostCode();
            rows[i][5] = customerData.get(i).getCity();
            rows[i][6] = customerData.get(i).getPhone();
            rows[i][7] = customerData.get(i).getEmail();
            rows[i][8] = customerData.get(i).getNip();
            rows[i][9] = customerData.get(i).getCreationDate();
            rows[i][10] = customerData.get(i).getModificationDate();
     }
    
    TableModel model = new DefaultTableModel(rows, columns);
    KlientTable.setModel(model); 
    final TableRowSorter<TableModel> sorter;
    sorter = new TableRowSorter<>(model);
    KlientTable.setRowSorter(sorter);
    KlientPanel.repaint();
    
    if(CustOptTxtField != null)
    CustOptTxtField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            String expr = CustOptTxtField.getText();
            sorter.setRowFilter(RowFilter.regexFilter(expr));
            sorter.setSortKeys(null);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            String expr = CustOptTxtField.getText();
            sorter.setRowFilter(RowFilter.regexFilter(expr));
            sorter.setSortKeys(null);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            String expr = CustOptTxtField.getText();
            sorter.setRowFilter(RowFilter.regexFilter(expr));
            sorter.setSortKeys(null);
        }
    });
    }
 
    public void addNewCustomer(final JTabbedPane MainTabbedPane){
        JPanel newCustPanel = new JPanel(new GridLayout(customerRows, customerColumn));
        
        JPanel mainPanel = new JPanel();
        mainPanel.add(newCustPanel, BorderLayout.CENTER);
        
        newCustPanel.add(new JLabel("Imię:"));
        txtFirstName = new JTextField(20);
        lblWarnFirstName = new JLabel();
        lblWarnFirstName.setForeground(Color.red);
        newCustPanel.add(txtFirstName);
        newCustPanel.add(lblWarnFirstName);
        
        newCustPanel.add(new JLabel("Nazwisko:"));
        txtLastName = new JTextField(20);
        lblWarnLastName = new JLabel();
        lblWarnLastName.setForeground(Color.red);
        newCustPanel.add(txtLastName);
        newCustPanel.add(lblWarnLastName);
        
        newCustPanel.add(new JLabel("Ulica:"));
        txtStreet = new JTextField(20);
        lblWarnStreet = new JLabel();
        lblWarnStreet.setForeground(Color.red);
        newCustPanel.add(txtStreet);
        newCustPanel.add(lblWarnStreet);
        
        newCustPanel.add(new JLabel("Kod pocztowy:"));
        txtZipCode = new JTextField(20);
        lblWarnZipCode = new JLabel();
        lblWarnZipCode.setForeground(Color.red);
        newCustPanel.add(txtZipCode);
        newCustPanel.add(lblWarnZipCode);
        
        newCustPanel.add(new JLabel("Miejscowość:"));
        txtCity = new JTextField(20);
        lblWarnCity = new JLabel();
        lblWarnCity.setForeground(Color.red);
        newCustPanel.add(txtCity); 
        newCustPanel.add(lblWarnCity);
        
        newCustPanel.add(new JLabel("Telefon:"));
        txtPhone = new JTextField(20);
        lblWarnPhone = new JLabel();
        lblWarnPhone.setForeground(Color.red);
        newCustPanel.add(txtPhone);   
        newCustPanel.add(lblWarnPhone);   
        
        newCustPanel.add(new JLabel("Email:"));
        txtEmail = new JTextField(20);
        lblWarnEmail = new JLabel();
        lblWarnEmail.setForeground(Color.red);
        newCustPanel.add(txtEmail);
        newCustPanel.add(lblWarnEmail);
        
        newCustPanel.add(new JLabel("NIP:"));
        txtNIP = new JTextField(20);
        lblWarnNIP = new JLabel();
        lblWarnNIP.setForeground(Color.red);
        newCustPanel.add(txtNIP); 
        newCustPanel.add(lblWarnNIP); 
        
        JPanel toolsCustPanel = new JPanel();
        JButton btnSaveCustomer = new JButton("Zapisz");
        btnSaveCustomer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if( validateCustFields() ){
                    utils.saveNewCustomer( setNewCustomerMap() );
                    MainTabbedPane.remove( MainTabbedPane.indexOfTab(customerTabName) );
                    MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerMainTab) );
                }
            }
        });
        toolsCustPanel.add(btnSaveCustomer);
        
        JButton btnCancelCustomerSave = new JButton("Anuluj zapis");
        btnCancelCustomerSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainTabbedPane.remove( MainTabbedPane.indexOfTab(customerTabName) );
                MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerMainTab) );
            }
        });
        toolsCustPanel.add(btnCancelCustomerSave);
        mainPanel.add(toolsCustPanel, BorderLayout.CENTER);
        
        MainTabbedPane.add(customerTabName, mainPanel);
        MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerTabName) );
        
    }

    private boolean validateCustFields(){
        boolean isValid = true;
        
        if(txtFirstName.getText().isEmpty()){
            lblWarnFirstName.setText("Imię jest wymagane");
            isValid = false;
        }else{
            lblWarnFirstName.setText("");
            isValid = true;            
        }
        
        if(txtLastName.getText().isEmpty()){
            lblWarnLastName.setText("Nazwisko jest wymagane");
            isValid = false;
        }else{
            lblWarnLastName.setText("");
            isValid = true;            
        }
        
        if(txtPhone.getText().isEmpty()){
            lblWarnPhone.setText("Telefon jest wymagany");
            isValid = false;
        }else{
            lblWarnPhone.setText("");
            isValid = true;  
        }
        return isValid;
    }
    
    private HashMap<String, String> setNewCustomerMap(){
        HashMap<String, String> customerMap = new HashMap<>();
        if(!txtFirstName.getText().isEmpty()) customerMap.put(CustomerEnum.firstName.name(), txtFirstName.getText());
        if(!txtLastName.getText().isEmpty()) customerMap.put(CustomerEnum.lastName.name(), txtLastName.getText());
        if(!txtStreet.getText().isEmpty()) customerMap.put(CustomerEnum.street.name(), txtStreet.getText());
        if(!txtZipCode.getText().isEmpty()) customerMap.put(CustomerEnum.postCode.name(), txtZipCode.getText());
        if(!txtCity.getText().isEmpty()) customerMap.put(CustomerEnum.city.name(), txtCity.getText());
        if(!txtPhone.getText().isEmpty()) customerMap.put(CustomerEnum.phone.name(), txtPhone.getText());        
        if(!txtEmail.getText().isEmpty()) customerMap.put(CustomerEnum.email.name(), txtEmail.getText());     
        if(!txtNIP.getText().isEmpty()) customerMap.put(CustomerEnum.nip.name(), txtNIP.getText()); 
        return customerMap;
    }
    
    public void showDialogForUser(String textToShow){
        JOptionPane.showMessageDialog(null, textToShow);
    }
    
    public void editCustomerData(final JTabbedPane MainTabbedPane, final int idCustomer){
        
      if(idCustomer > 0){
        JPanel newCustPanel = new JPanel(new GridLayout(customerRows, customerColumn));
        
        JPanel mainPanel = new JPanel();
        mainPanel.add(newCustPanel, BorderLayout.CENTER);
        
        newCustPanel.add(new JLabel("Imię:"));
        txtFirstName = new JTextField(20);
        lblWarnFirstName = new JLabel();
        lblWarnFirstName.setForeground(Color.red);
        newCustPanel.add(txtFirstName);
        newCustPanel.add(lblWarnFirstName);
        
        newCustPanel.add(new JLabel("Nazwisko:"));
        txtLastName = new JTextField(20);
        lblWarnLastName = new JLabel();
        lblWarnLastName.setForeground(Color.red);
        newCustPanel.add(txtLastName);
        newCustPanel.add(lblWarnLastName);
        
        newCustPanel.add(new JLabel("Ulica:"));
        txtStreet = new JTextField(20);
        lblWarnStreet = new JLabel();
        lblWarnStreet.setForeground(Color.red);
        newCustPanel.add(txtStreet);
        newCustPanel.add(lblWarnStreet);
        
        newCustPanel.add(new JLabel("Kod pocztowy:"));
        txtZipCode = new JTextField(20);
        lblWarnZipCode = new JLabel();
        lblWarnZipCode.setForeground(Color.red);
        newCustPanel.add(txtZipCode);
        newCustPanel.add(lblWarnZipCode);
        
        newCustPanel.add(new JLabel("Miejscowość:"));
        txtCity = new JTextField(20);
        lblWarnCity = new JLabel();
        lblWarnCity.setForeground(Color.red);
        newCustPanel.add(txtCity); 
        newCustPanel.add(lblWarnCity);
        
        newCustPanel.add(new JLabel("Telefon:"));
        txtPhone = new JTextField(20);
        lblWarnPhone = new JLabel();
        lblWarnPhone.setForeground(Color.red);
        newCustPanel.add(txtPhone);   
        newCustPanel.add(lblWarnPhone);   
        
        newCustPanel.add(new JLabel("Email:"));
        txtEmail = new JTextField(20);
        lblWarnEmail = new JLabel();
        lblWarnEmail.setForeground(Color.red);
        newCustPanel.add(txtEmail);
        newCustPanel.add(lblWarnEmail);
        
        newCustPanel.add(new JLabel("NIP:"));
        txtNIP = new JTextField(20);
        lblWarnNIP = new JLabel();
        lblWarnNIP.setForeground(Color.red);
        newCustPanel.add(txtNIP); 
        newCustPanel.add(lblWarnNIP); 
        
     
        fillCustomerControlers(idCustomer);
        
        JPanel toolsCustPanel = new JPanel();
        JButton btnSaveCustomer = new JButton("Zapisz");
        btnSaveCustomer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Customer customer = updateCustomerData(idCustomer);
                utils.updateCustomer(customer);
                MainTabbedPane.remove( MainTabbedPane.indexOfTab(customerTabNameEdition) );
                MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerMainTab) );
            }
        });  
        
        JButton btnCancelCustomerSave = new JButton("Anuluj edycję");
        btnCancelCustomerSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainTabbedPane.remove( MainTabbedPane.indexOfTab(customerTabNameEdition) );
                MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerMainTab) );
            }
        });
        toolsCustPanel.add(btnCancelCustomerSave);
        toolsCustPanel.add(btnSaveCustomer);
        mainPanel.add(toolsCustPanel);
        MainTabbedPane.add(customerTabNameEdition, mainPanel);
        MainTabbedPane.setSelectedIndex( MainTabbedPane.indexOfTab(customerTabNameEdition) );
      }
  }
    
    private void fillCustomerControlers(int idCustomer){
        Customer customer = utils.getCustomerById(idCustomer);
        txtFirstName.setText(customer.getFirstName());
        txtLastName.setText(customer.getLastName());
        txtStreet.setText(customer.getStreet());
        txtCity.setText(customer.getCity());
        txtZipCode.setText(customer.getPostCode());
        txtPhone.setText(customer.getPhone());
        txtNIP.setText(customer.getNip());
        txtEmail.setText(customer.getEmail());
    }
    
    private Customer updateCustomerData(int idCustomer){
        Customer customer = utils.getCustomerById(idCustomer);
        if(!txtFirstName.getText().equals(customer.getFirstName())){
            customer.setFirstName(txtFirstName.getText());
        }
        if(!txtLastName.getText().equals(customer.getLastName())){
            customer.setLastName(txtLastName.getText());
        }
        if(!txtStreet.getText().equals(customer.getStreet())){
            customer.setStreet(txtStreet.getText());
        }
        if(!txtCity.getText().equals(customer.getCity())){
            customer.setCity(txtCity.getText());
        }
        if(!txtEmail.getText().equals(customer.getEmail())){
            customer.setEmail(txtEmail.getText());
        }
        if(!txtNIP.getText().equals(customer.getNip())){
            customer.setNip(txtNIP.getText());
        }
        if(!txtPhone.getText().equals(customer.getPhone())){
            customer.setPhone(txtPhone.getText());
        }
        if(!txtZipCode.getText().equals(customer.getPostCode())){
            customer.setPostCode(txtZipCode.getText());
        }

        customer.setModificationDate(new Date());
        return customer;
    }
    
    public void deleteCustomer(int idCustomer){
        utils.deleteCustomer(idCustomer);
    }
}